package com.mine.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.dao.MinePublishContentCategoryMapper;
import com.mine.dao.MinePublishContentDetailMapper;
import com.mine.dao.MinePublishContentMapper;
import com.mine.pojo.MinePublishContent;
import com.mine.pojo.MinePublishContentCategory;
import com.mine.pojo.MinePublishContentCategoryExample;
import com.mine.pojo.MinePublishContentCategoryExample.Criteria;
import com.mine.pojo.MinePublishContentDetail;
import com.mine.pojo.MinePublishContentDetailExample;
import com.mine.pojo.MinePublishContentExample;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.vo.PublishVo;

@Service
public class PublishServiceImpl implements PublishService {

	@Autowired
	private UserService userService;
	@Autowired
	private MinePublishContentCategoryMapper contentCategoryMapper; 
	@Autowired
	private MinePublishContentMapper contentMapper;
	@Autowired
	private MinePublishContentDetailMapper contentDetailMapper;
	
	@Override
	public Result addPublishContent(String token, PublishVo publishVo) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	//查询分类是否存在
    	MinePublishContentCategory category = contentCategoryMapper.selectByPrimaryKey(publishVo.getCategoryId());
    	if (category==null) {
			return Result.error("分类不存在！");
		}
    	
    	MinePublishContent content = new MinePublishContent();
    	content.setCategoryId(publishVo.getCategoryId());
    	content.setCreateTime(new Date());
    	content.setUserId(redisUser.getId());
    	content.setImgUrls(publishVo.getImgUrls());
    	content.setShortContent(publishVo.getShortContent());
    	content.setTitle(publishVo.getTitle());
    	
    	contentMapper.insertSelective(content);
    	
    	MinePublishContentDetail publishContentDetail = new MinePublishContentDetail();
    	publishContentDetail.setCategoryId(publishVo.getCategoryId());
    	publishContentDetail.setContent(publishVo.getContent());
    	publishContentDetail.setContentId(content.getId());
    	publishContentDetail.setTitle(publishVo.getTitle());
    	publishContentDetail.setUserId(redisUser.getId());
    	contentDetailMapper.insertSelective(publishContentDetail);

    	//返回新增的内容id
		return Result.success(content.getId());
	}

	@Override
	public Result addPublishCategory(String token, MinePublishContentCategory contentCategory) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	contentCategory.setUserId(redisUser.getId());
    	contentCategory.setCreateTime(new Date());
    	contentCategory.setUpdateTime(new Date());
    	int i = contentCategoryMapper.insertSelective(contentCategory);
    	if (i>0) {
			return Result.ok();
		}
		return Result.error("添加失败！");
	}

	@Override
	public Result deletePublishCategory(String token, MinePublishContentCategory contentCategory) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	contentCategoryMapper.deleteByPrimaryKey(contentCategory.getId());
		return Result.ok();
	}

	@Override
	public Result updatePublishCategory(String token, MinePublishContentCategory contentCategory) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
		return Result.ok();
	}

	@Override
	public Result getPublishCategory(String token) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	MinePublishContentCategoryExample example = new MinePublishContentCategoryExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andStatusEqualTo((byte) 1);
    	criteria.andUserIdEqualTo(redisUser.getId());
    	List<MinePublishContentCategory> list = contentCategoryMapper.selectByExample(example);
    	if (list!=null&&list.size()>0) {
			return Result.success(list);
		}
		return Result.error("查询失败！");
	}

	@Override
	public Result updatePublishContent(String token, PublishVo publishVo) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	//检查该id对应的数据是否存在
    	MinePublishContent publishContent = contentMapper.selectByPrimaryKey(publishVo.getId());
    	if (publishContent==null) {
			return Result.error("数据不存在！");
		}
    	//查询分类是否存在
    	MinePublishContentCategory category = contentCategoryMapper.selectByPrimaryKey(publishVo.getCategoryId());
    	if (category==null) {
			return Result.error("分类不存在！");
		}
    	//插入内容表
    	MinePublishContent content = new MinePublishContent();
    	content.setCategoryId(publishVo.getCategoryId());
    	content.setCreateTime(new Date());
    	content.setUserId(redisUser.getId());
    	content.setImgUrls(publishVo.getImgUrls());
    	content.setShortContent(publishVo.getShortContent());
    	content.setTitle(publishVo.getTitle());
    	content.setId(publishVo.getId());
    	contentMapper.updateByPrimaryKeySelective(content);
    	//插入内容详情表
    	MinePublishContentDetail publishContentDetail = new MinePublishContentDetail();
    	publishContentDetail.setCategoryId(publishVo.getCategoryId());
    	publishContentDetail.setContent(publishVo.getContent());
    	publishContentDetail.setContentId(publishVo.getId());
    	publishContentDetail.setTitle(publishVo.getTitle());
    	publishContentDetail.setUserId(redisUser.getId());
    	contentDetailMapper.updateByPrimaryKeySelective(publishContentDetail);
		return Result.ok();
	}

	@Override
	public Result deletePublishContent(String token, PublishVo publishVo) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	contentMapper.deleteByPrimaryKey(publishVo.getId());
    	contentDetailMapper.deleteByPrimaryKey(publishVo.getId());
		return Result.ok();
	}

	@Override
	public Result getPublishContent(String token, PublishVo publishVo) {
		//先验证用户信息
    	MineUser redisUser = userService.getRedisUserByToken(token);
    	if (redisUser==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
    	MinePublishContent publishContent = contentMapper.selectByPrimaryKey(publishVo.getId());
    	if (publishContent==null) {
			return Result.error("数据不存在！");
		}
    	MinePublishContentDetail publishContentDetail = contentDetailMapper.selectByPrimaryKey(publishVo.getId());
    	HashMap<String, Object> map =new HashMap<>();
    	map.put("content", publishContent);
    	map.put("contenDes", publishContentDetail.getContent());
		return Result.success(map);
	}

	@Override
	public Result getPublishContentList(String token, Long categoryId) {
		MinePublishContentExample example = new MinePublishContentExample();
		com.mine.pojo.MinePublishContentExample.Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		List<MinePublishContent> list = contentMapper.selectByExample(example);
		if (list!=null&&list.size()>0) {
			return Result.success(list);
		}
		return Result.error("暂无数据。。");
	}

}
